﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using UBConDemo.Models;

namespace UBConDemo
{
    public class UbConItemTemplateSelector:DataTemplateSelector
    {
        DataTemplate _headerTemplate = null, _contentTemplate;
        
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (_headerTemplate == null||_contentTemplate==null) 
            {
                ResourceDictionary dictionary = new ResourceDictionary() 
                {
                    Source = new Uri("/UbConDemo;component/Themes/DataTemplates.xaml", UriKind.Relative)
                };

                ComponentResourceKey _headerTemplateKey = new ComponentResourceKey()
                {
                    TypeInTargetAssembly = typeof(ResourceManager),
                    ResourceId = "ControlHeaderTemplate"
                };

                ComponentResourceKey _contentTemplateKey = new ComponentResourceKey()
                {
                    TypeInTargetAssembly = typeof(ResourceManager),
                    ResourceId = "ControlContentTemplate"
                };

                _headerTemplate = dictionary[_headerTemplateKey] as DataTemplate;
                _contentTemplate = dictionary[_contentTemplateKey] as DataTemplate;
            }

            UbConItemModel _model=item as UbConItemModel;

            if (_model.IsCategoryNameItem)
            {
                return _headerTemplate;
            }

            else 
            {
                return _contentTemplate;
            }
        }
    }
}
